package gui;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import common.SpinBox;
import common.TVConsts;

import server.ClockServer;
import server.ImageServer;
import server.SpinBoxServer;

@SuppressWarnings("serial")
public class ServerGui extends JFrame implements ActionListener {
	Icon icon = null;

	ImageServer imgsrv = new ImageServer(TVConsts.CH1_PORT);
	ClockServer clksrv = new ClockServer(TVConsts.CH2_PORT);
	SpinBoxServer sbsrv = new SpinBoxServer(TVConsts.CH3_PORT);

	Timer timer; // Swing的控件，用来刷新界面

	JLabel lblImg = new JLabel(); // image show
	Canvas cvImg = new Canvas(); // spinBox显示
	JButton btnImgSwitch = new JButton("开/关"), btnImgSelect = new JButton(
			"选择图片"), btnTimeStart = new JButton("开/关"),
			btnTimeSet = new JButton("设置时间"), btnSpinBoxSwitch = new JButton(
					"开/关"), btnSpinBoxSet = new JButton("设置角度");
	JTextField txtTime = new JTextField(12);
	JTextArea txtLog = new JTextArea(20, 8);

	public ServerGui() {
		super("服务器");

		btnTimeStart.addActionListener(this);
		btnTimeSet.addActionListener(this);
		this.btnImgSwitch.addActionListener(this);
		this.btnImgSelect.addActionListener(this);
		this.btnSpinBoxSwitch.addActionListener(this);
		this.btnSpinBoxSet.addActionListener(this);

		this.setLayout(new GridLayout(3, 1));

		JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p1.add(this.btnImgSwitch);
		p1.add(this.btnImgSelect);
		p1.add(lblImg);
		this.add(p1);

		JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p2.add(btnTimeStart);
		p2.add(btnTimeSet);
		p2.add(txtTime);
		this.add(p2);

		JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p3.add(btnSpinBoxSwitch);
		p3.add(btnSpinBoxSet);
		cvImg.setSize(80, 80);
		p3.add(cvImg);
		this.add(p3);

		this.setLocation(100, 100);
		this.setSize(600, 500);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		int delay = 200; // 200ms
		ActionListener taskPerformer = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				refresh();
			}
		};
		timer = new Timer(delay, taskPerformer);
		timer.start();
	}

	// 界面刷新函数
	public void refresh() {
		if (icon != null) {
			lblImg.setIcon(icon);
		}

		txtTime.setText(clksrv.getTimeString());// 刷新时间显示
		SpinBox.paint(sbsrv.getSpinBox(), cvImg.getGraphics(), 80);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == btnTimeStart) {
			try {
				if (!clksrv.isRunning()) {
					clksrv.start();
					this.btnTimeStart.setBackground(Color.GREEN);
				} else {
					clksrv.stop();
					this.btnTimeStart.setBackground(Color.WHITE);
				}
			} catch (Exception ex) {
				System.out.println("服务开启失败");
				JOptionPane.showMessageDialog(btnTimeStart, "服务开启失败");
			}
		}
		if (source == btnTimeSet) {
			String s = JOptionPane.showInputDialog(btnTimeSet, new String(
					"input time like 12:10:10"), "设置时间",
					JOptionPane.OK_CANCEL_OPTION);
			if (s != null) {
				clksrv.setTime(s);
				txtTime.setText(clksrv.getTimeString());
			}
		}
		if (source == this.btnImgSwitch) {

			if (!imgsrv.isRunning()) {
				imgsrv.start();
				this.btnImgSwitch.setBackground(Color.GREEN);
			} else {
				imgsrv.stop();
				this.btnImgSwitch.setBackground(Color.WHITE);
			}
		}
		if (source == this.btnImgSelect) {
			JFileChooser fc = new JFileChooser();
			// fc.setFileFilter(new FileNameExtensionFilter("位图图像(*.bmp)",
			// "bmp"));
			int retval = fc.showOpenDialog(this);
			if (retval == JFileChooser.APPROVE_OPTION) {
				try {
					icon = new ImageIcon(ImageIO.read(fc.getSelectedFile()));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				imgsrv.setImage(fc.getSelectedFile());
			}

		}
		if (source == this.btnSpinBoxSwitch) {
			try {
				if (!sbsrv.isRunning()) {
					sbsrv.start();
					this.btnSpinBoxSwitch.setBackground(Color.GREEN);
				} else {
					sbsrv.stop();
					this.btnSpinBoxSwitch.setBackground(Color.WHITE);
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		if (source == this.btnSpinBoxSet) {
			String s = JOptionPane.showInputDialog(this, "请输入角度");
			if (s != null) {
				sbsrv.getSpinBox().setDegree(Double.parseDouble(s));
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ServerGui();
	}

}
